package com.ruipeng.shengyapo.locksocket;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;

/**
 * Created by TreeJohn on 17-5-8.
 */
public class NettyChannelMap {

    private static Logger log = Logger.getLogger("lock");

	private static Map<String, SocketChannel> map = new ConcurrentHashMap<String, SocketChannel>();

	public static void add(String clientId, SocketChannel socketChannel) {
		map.put(clientId, socketChannel);
	}

	public static Channel get(String clientId) {
		return map.get(clientId);
	}
	
	public static void print() {
        log.info("map: " + map);
		//System.out.println("map: " + map);

		for (String key : map.keySet()) {
			System.out.println("key: " + key + " value: " + map.get(key));
		}
	}

	@SuppressWarnings("rawtypes")
	public static void remove(SocketChannel socketChannel) {
		for (Map.Entry entry : map.entrySet()) {
			if (entry.getValue() == socketChannel) {
				map.remove(entry.getKey());
			}
		}
	}

}
